﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	canvas3D
 *      @author: CYS
 */



var polygon = function()
{
	this.v = [];
	this.editable = false;

}

polygon.prototype.draw = function(renderer)
{
	var len = this.v.length;

	if(len > 0)
	{
		renderer.fillStyle = "#f00";
		renderer.beginPath();
		renderer.moveTo(this.v[0][0], this.v[0][1]);

		for(var i = 1; i < len; i++) renderer.lineTo(this.v[i][0], this.v[i][1]);

		renderer.closePath();
		renderer.fill();
	}
}

polygon.prototype.add = function(v)
{
	this.v.push(v);
}

polygon.prototype.addRect = function(x, y, w, h)
{
	this.v[0] = [x, y];
	this.v[1] = [x, y + h];
	this.v[2] = [x + w, y + h];
	this.v[3] = [x + w, y + h];
}



var canvas3D = function(ctx)
{
	this.ctx = ctx;
	this.geo;
	this.trf;
	this.img;
}


canvas3D.prototype.setGeometry = function(g)
{
	//this.geo = g;
	this.geo =
	    [
	        0, 0, 1,
	        0.5, 0.5, 1,
	        -0.5, 0.5, 1,
	    ];
}


canvas3D.prototype.setTrasform = function(t)
{
	this.trf = t;
}


canvas3D.prototype.draw = function()
{
	for(var i = 0; i < this.geo.length(); i++)
	{
		//this.geo[i] = mul(this.geo[i], this.trf);
	}
}



var points = function()
{
	this.v = [];

}

points.prototype.init = function(num)
{
	for(var i = 0; i < num; i++)
	{
		var x = rndab(-width*2, width*2);
		var y = rndab(-width*2, width*2);
		var z = rndab(0, width*10);
		var p = [x, y, z];
		this.v[i] = p;
	}
}


points.prototype.init2 = function(num)
{
	for(var i = 0; i < num; i++)
	{
		var x = rndab(-width*2, width*2);
		var y = rndab(-width*2, width*2);
		var z = rndab(width*4, width*8);
		var p = [x, y, z];
		this.v[i] = p;
	}
}




points.prototype.draw = function(nearPlane, renderer)
{
	renderer.fillStyle = "#fff";
	var size = 10;

	for(var i = 0; i < this.v.length; i++)
	{
		var p = this.v[i];
		p[2] -= dtime;

		if(p[2] < nearPlane) p[2] = rnd(width*7, width*10);

		//project to screen
		var s = nearPlane/this.v[i][2];
		var x = s * this.v[i][0] + width/2;
		var y = s * this.v[i][1] + height/2;

		// draw a point
		var t = 20;
		//renderer.fillStyle = rgba(s*5*t+5, s*25*t+25, s*60*t+60, 1);
		//renderer.beginPath();
		//renderer.arc(x, y, s*size, 0, 2*Math.PI, true);
		//renderer.closePath();
		renderer.fillRect(x, y, s*size, s*size);
		renderer.fill();
	}
}


points.prototype.draw2 = function(nearPlane, angle, renderer)
{
	renderer.fillStyle = "#fff";
	var size = 10;

	for(var i = 0; i < this.v.length; i++)
	{
		var x = this.v[i][0];
		var y = this.v[i][1];
		var z = this.v[i][2];
		z -= width*6;

		var xr = x*cos(angle) - z*sin(angle);
		var zr = x*sin(angle) + z*cos(angle);
		z = zr + width*6;
		x = xr;
		//project to screen
		var s = nearPlane/z;
		var x = s * x + width/2;
		var y = s * y + height/2;

		// draw a point
		var t = 20;
		//renderer.fillStyle = rgba(s*5*t+5, s*25*t+25, s*60*t+60, 1);
		//renderer.beginPath();
		//renderer.arc(x, y, s*size, 0, 2*Math.PI, true);
		//renderer.closePath();
		renderer.fillRect(x, y, s*size, s*size);
		renderer.fill();
	}
}



points.prototype.init3 = function(halfSize)
{
	this.v =
	    [
	        // Front face
	        [-halfSize, -halfSize,  halfSize],
	        [halfSize, -halfSize,  halfSize],
	        [halfSize,  halfSize,  halfSize],
	        [-halfSize,  halfSize,  halfSize],

	        // Back face
	        [-halfSize, halfSize, -halfSize],
	        [halfSize,  halfSize, -halfSize],
	        [halfSize,  -halfSize, -halfSize],
	        [-halfSize, -halfSize, -halfSize],

	        // Top face
	        [-halfSize,  halfSize, -halfSize],
	        [-halfSize,  halfSize,  halfSize],
	        [halfSize,  halfSize,  halfSize],
	        [halfSize,  halfSize, -halfSize],

	        // Bottom face
	        [-halfSize, -halfSize, -halfSize],
	        [halfSize, -halfSize, -halfSize],
	        [halfSize, -halfSize,  halfSize],
	        [-halfSize, -halfSize,  halfSize],

	        // Right face
	        [halfSize, -halfSize,  halfSize],
	        [halfSize, -halfSize, -halfSize],
	        [halfSize,  halfSize, -halfSize],
	        [halfSize,  halfSize,  halfSize],

	        // Left face
	        [-halfSize, -halfSize, -halfSize],
	        [-halfSize, -halfSize,  halfSize],
	        [-halfSize,  halfSize,  halfSize],
	        [-halfSize,  halfSize, -halfSize]
	    ];

	this.uv =
	    [
	        // Front face
	        0.0, 0.0,
	        1.0, 0.0,
	        1.0, 1.0,
	        0.0, 1.0,

	        // Back face
	        1.0, 0.0,
	        1.0, 1.0,
	        0.0, 1.0,
	        0.0, 0.0,

	        // Top face
	        0.0, 1.0,
	        0.0, 0.0,
	        1.0, 0.0,
	        1.0, 1.0,

	        // Bottom face
	        1.0, 1.0,
	        0.0, 1.0,
	        0.0, 0.0,
	        1.0, 0.0,

	        // Right face
	        1.0, 0.0,
	        1.0, 1.0,
	        0.0, 1.0,
	        0.0, 0.0,

	        // Left face
	        0.0, 0.0,
	        1.0, 0.0,
	        1.0, 1.0,
	        0.0, 1.0,
	    ];

}

points.prototype.proj3DPoint =function(p, nearPlane, angle, renderer)
{
	var x = p[0];
	var y = p[1];
	var z = p[2];

	var xr = x*cos(angle) - z*sin(angle);
	var zr = x*sin(angle) + z*cos(angle);
	z = zr + width*6;
	x = xr;
	//project to screen
	var s = nearPlane/z;
	var sx = s * x + width/2;
	var sy = s * y + height/2;

	//draw a point
	//var size = 30;
	//renderer.fillStyle = "#fff";
	//renderer.beginPath();
	//renderer.arc(sx, sy, s*size, 0, 2*Math.PI, true);
	//renderer.closePath();
	//renderer.fill();
	return [~~sx, ~~sy, x, y, z];
}


points.prototype.draw3 = function(nearPlane, angle, renderer)
{
	renderer.fillStyle = "#fff";
	renderer.strokeStyle = "#fff";
	renderer.lineWidth = 0.25;

	for(var i = 0; i < this.v.length; )
	{
		var p0 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p1 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p2 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p3 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		renderer.beginPath();
		renderer.moveTo(p0[0], p0[1]);
		renderer.lineTo(p1[0], p1[1]);
		renderer.lineTo(p2[0], p2[1]);
		renderer.lineTo(p3[0], p3[1]);
		renderer.closePath();
		renderer.stroke();
	}

}


points.prototype.draw4 = function(nearPlane, angle, renderer)
{
	renderer.fillStyle = "#fff";
	renderer.strokeStyle = "#fff";
	renderer.lineWidth = 0.5;

	for(var i = 0; i < this.v.length; )
	{
		var p0 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p1 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p2 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p3 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);

		var v0 = new vec3(p0[2], p0[3], p0[4] );
		var v1 = new vec3(p1[2], p1[3], p1[4] );
		var v2 = new vec3(p2[2], p2[3], p2[4] );
		v1.sub(v0);
		v2.sub(v0);
		var n = cross(v1, v2);
		n.normalize();
		var see = -n.dot(v0.add(v1));
		var c = Math.abs(n.e[2]);

		//var c = Math.abs(see);
		if(see > 0)
		{
			renderer.fillStyle = rgba(c*255, c*255, c*255, 1);

			renderer.beginPath();
			renderer.moveTo(p0[0], p0[1]);
			renderer.lineTo(p1[0], p1[1]);
			renderer.lineTo(p2[0], p2[1]);
			renderer.lineTo(p3[0], p3[1]);
			renderer.closePath();
			renderer.fill();
			//renderer.stroke();

		}

	}

	for(var i = 0; i < this.v.length; )
	{
		var p0 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p1 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p2 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p3 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);

		var v0 = new vec3(p0[2], p0[3], p0[4] );
		var v1 = new vec3(p1[2], p1[3], p1[4] );
		var v2 = new vec3(p2[2], p2[3], p2[4] );
		v1.sub(v0);
		v2.sub(v0);
		var n = cross(v1, v2);
		n.normalize();
		var see = -n.dot(v0.add(v1));
		var c = Math.abs(n.e[2]);
		//var c = Math.abs(see);
		//if(see < 0)
		//{
		//	renderer.fillStyle = rgba(c*255, c*255, c*255, 1);

		//	renderer.beginPath();
		//	renderer.moveTo(p0[0], p0[1]);
		//	renderer.lineTo(p1[0], p1[1]);
		//	renderer.lineTo(p2[0], p2[1]);
		//	renderer.lineTo(p3[0], p3[1]);
		//	renderer.closePath();
		//	renderer.fill();
		//	//renderer.stroke();

		//}

	}

}



points.prototype.draw5 = function(nearPlane, angle, renderer)
{
	renderer.fillStyle = "#fff";
	renderer.strokeStyle = "#fff";
	renderer.lineWidth = 0.25;

	for(var i = 0; i < this.v.length; )
	{
		var p0 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p1 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p2 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		var p3 = this.proj3DPoint(this.v[i++], nearPlane, angle, renderer);
		//renderer.beginPath();
		//renderer.moveTo(p0[0], p0[1]);
		//renderer.lineTo(p1[0], p1[1]);
		//renderer.lineTo(p2[0], p2[1]);
		//renderer.lineTo(p3[0], p3[1]);
		//renderer.closePath();
		//renderer.stroke();

		var v0 = new vec3(p0[2], p0[3], p0[4] );
		var v1 = new vec3(p1[2], p1[3], p1[4] );
		var v2 = new vec3(p2[2], p2[3], p2[4] );
		v1.sub(v0);
		v2.sub(v0);
		var n = cross(v1, v2);
		n.normalize();
		var see = -n.dot(v0.add(v1));
		var c = Math.abs(n.e[2]);

		//var c = Math.abs(see);
		if(see > 0)
		{
			var dx = p1[0] - p0[0];
			var sliceCount = (Math.abs(dx));

			var dy0, dy1, dimgx;

			if(sliceCount > 0)
			{
				//renderer.drawImage(imgUV, 0, 0, imgUV.width, 1,
				//				p3[0], p3[1], dx, p0[1]+j*dy1 - p3[1]-j*dy0)


				dx = dx > 0 ? 1 : -1;//dx/sliceCount;
				dy0 = (p2[1] - p3[1])/sliceCount;
				dy1 = (p1[1] - p0[1])/sliceCount;
				dimgx = imgUV.width/sliceCount;



				for(var j = 0; j < sliceCount; j++)
				{
					var imgS = ~~(dimgx*j);
					var imgW = Math.ceil(dimgx*(j+1)) - imgS;
					renderer.drawImage(imgUV, imgS, 0, imgW, imgUV.height,
					                   p3[0]+j*dx, p3[1]+j*dy0, dx, p0[1]+j*dy1 - p3[1]-j*dy0);
					//renderer.beginPath();
					//renderer.moveTo(p3[0]+j*dx, p3[1]+j*dy0);
					//renderer.lineTo(p0[0]+j*dx, p0[1]+j*dy1);
					//renderer.closePath();
					//renderer.stroke();

				}

			}
		}

	}


}


function getDataFromImage(img, width, height)
{
	var canvas = document.createElement('canvas');
	canvas.width = width || img.width;
	canvas.height = height || img.height;
	var ctx = canvas.getContext('2d');
	ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
	return ctx.getImageData(0, 0, img.width, img.height);
};


//function drawLine(canva)
